package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.BooleanUtil;
import com.sc.nft.dao.EcologyHatchExchangeConfDao;
import com.sc.nft.dao.EcologyHatchUsePropsExchangeDao;
import com.sc.nft.dao.EquityPropsDao;
import com.sc.nft.dao.UserEquityPropsDao;
import com.sc.nft.dto.EcologyHatchConfDao;
import com.sc.nft.dto.UserEcologyHatchRecordsDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.EcologyHatchCountDTO;
import com.sc.nft.entity.req.EcologyHatchExchangeConfRequest;
import com.sc.nft.entity.vo.AdminEcologyHatchExchangeConfVO;
import com.sc.nft.entity.vo.AdminEcologyHatchUsePropsExchangeVO;
import com.sc.nft.entity.vo.EcologyHatchExchangeConfVO;
import com.sc.nft.entity.vo.EcologyHatchUsePropsExchangeVO;
import com.sc.nft.entity.vo.ecology.ExchangeCastUpperLimitConfVO;
import com.sc.nft.enums.UserEcologyCastUpperLimitDetailsTypeEnum;
import com.sc.nft.enums.UserEcologyHatchUpperLimitDetailsTypeEnum;
import com.sc.nft.enums.UserEquityPropsUseTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.*;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-08-06
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyHatchExchangeConfServiceImpl implements EcologyHatchExchangeConfService {

    private final EcologyHatchConfDao ecologyHatchConfDao;


    private final EcologyHatchExchangeConfDao ecologyHatchExchangeConfDao;

    private final EcologyHatchUsePropsExchangeService ecologyHatchUsePropsExchangeService;

    private final EcologyHatchUsePropsExchangeDao ecologyHatchUsePropsExchangeDao;

    private final EquityPropsDao equityPropsDao;

    private final UserEcologyHatchUpperLimitService userEcologyHatchUpperLimitService;

    private final UserEquityPropsService userEquityPropsService;

    private final UserEquityPropsDao userEquityPropsDao;

    private final UserEcologyHatchRecordsDao userEcologyHatchRecordsDao;

    private final EcologySmeltingService ecologySmeltingService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editEcologyHatchExchangeConf(EcologyHatchExchangeConfRequest req){
        EcologyHatchConf ecologyHatchConfInDb = ecologyHatchConfDao.getById(req.getEcologyHatchId());
        Assert.notNull(ecologyHatchConfInDb, () -> new GlobalRunTimeException("生态熔炼配置不存在"));
        //熔炼上限配置id
        Long confId = req.getId();
        if (Objects.isNull(confId)) {
            EcologyHatchExchangeConf ecologyHatchExchangeConf = BeanUtil.copyProperties(req, EcologyHatchExchangeConf.class);
            ecologyHatchExchangeConf.insert();
            confId = ecologyHatchExchangeConf.getId();

        }else {
            Assert.isTrue(ecologyHatchExchangeConfDao.updateEcologyHatchExchangeConfById(req.getId(),req.getSysGiftStatus(),req.getGiftLimit(),
                            req.getGiftCount(),req.getStatus()), () -> new GlobalRunTimeException("修改熔炼上限配置失败"));
        }
        //编辑熔炼上限道具关联表
        ecologyHatchUsePropsExchangeService.editEcologyHatchUsePropsExchange(req.getEcologyHatchId()
                ,confId,req.getEcologyHatchUsePropsExchangeRequestList());
    }

    /**
     * 兑换熔炼上限配置详情
     * @param ecologyHatchId
     * @return
     */
    @Override
    public AdminEcologyHatchExchangeConfVO findAdminEcologyHatchExchangeConfDetail(Long ecologyHatchId){

        EcologyHatchExchangeConf ecologyHatchExchangeConf = ecologyHatchExchangeConfDao.getOneByEcologyHatchId(ecologyHatchId);
        if (Objects.isNull(ecologyHatchExchangeConf)){
            return null;
        }
        //Assert.notNull(ecologyHatchExchangeConf, () -> new GlobalRunTimeException("兑换生态熔炼上限配置不存在"));
        AdminEcologyHatchExchangeConfVO hatchExchangeConfVO = BeanUtil.copyProperties(ecologyHatchExchangeConf, AdminEcologyHatchExchangeConfVO.class);
        List<EcologyHatchUsePropsExchange> listByExchangeList = ecologyHatchUsePropsExchangeDao.findAdminEcologyHatchUsePropsExchangeListByConfId(ecologyHatchExchangeConf.getId());
        List<AdminEcologyHatchUsePropsExchangeVO> ecologyHatchUsePropsExchangeVOS = BeanUtil.copyToList(listByExchangeList, AdminEcologyHatchUsePropsExchangeVO.class);
        hatchExchangeConfVO.setEcologyHatchUsePropsExchangeVOList(ecologyHatchUsePropsExchangeVOS);
        return hatchExchangeConfVO;
    }



    /**
     * 获取生态铸造上限兑换配置
     * @param userInfo
     * @param ecologyHatchId
     * @return
     */
    @Override
    public EcologyHatchExchangeConfVO getExchangeCastUpperLimitConfList(UserInfo userInfo, Long ecologyHatchId) {
        EcologyHatchConf ecologyHatchConf = ecologyHatchConfDao.getById(ecologyHatchId);
        Assert.notNull(ecologyHatchConf, () -> new GlobalRunTimeException("生态熔炼配置不存在"));
        //获取兑换熔炼上限配置
        EcologyHatchExchangeConf ecologyHatchExchangeConf = ecologyHatchExchangeConfDao.getExchangeConfByEcologyHatchId(ecologyHatchId);
        if (Objects.isNull(ecologyHatchExchangeConf)){
            return null;
        }
        //Assert.notNull(ecologyHatchExchangeConf, () -> new GlobalRunTimeException("生态兑换熔炼配置不存在"));

        EcologyHatchExchangeConfVO ecologyHatchExchangeConfVO = BeanUtil.copyProperties(ecologyHatchExchangeConf, EcologyHatchExchangeConfVO.class);
        ecologyHatchExchangeConfVO.setCollectionName(ecologyHatchConf.getCollectionName());
        ecologyHatchExchangeConfVO.setCollectionImage(ecologyHatchConf.getCollectionImage());
        //获取兑换熔炼上限配置道具卡
        List<EcologyHatchUsePropsExchange> ecologyHatchUsePropsExchangeList = ecologyHatchUsePropsExchangeService.
                findEcologyHatchUsePropsExchangeListByConfId(ecologyHatchExchangeConfVO.getId());
        if (CollectionUtils.isEmpty(ecologyHatchUsePropsExchangeList)){
            return null;
        }
        //Assert.notEmpty(ecologyHatchUsePropsExchangeList, () -> new GlobalRunTimeException("生态熔炼上限配置道具卡不存在"));
        List<EcologyHatchUsePropsExchangeVO> ecologyHatchUsePropsExchangeVOList = BeanUtil.copyToList(ecologyHatchUsePropsExchangeList, EcologyHatchUsePropsExchangeVO.class);
        ecologyHatchExchangeConfVO.setEcologyHatchUsePropsExchangeVOList(ecologyHatchUsePropsExchangeVOList);

        for (EcologyHatchUsePropsExchangeVO ecologyHatchUsePropsExchangeVO : ecologyHatchUsePropsExchangeVOList) {
            Integer count = userEquityPropsDao.getHavingCountByUserId(ecologyHatchUsePropsExchangeVO.getPropId(), userInfo.getId());
            ecologyHatchUsePropsExchangeVO.setHasCount(count);
        }
        return ecologyHatchExchangeConfVO;
    }




    /**
     * 熔炼上限兑换
     */
    @Override
    public void ecologyHatchExchange(Long ecologyHatchId,Long propId, Integer count, Long userId){

        EcologyHatchConf ecologyHatchConfInDb = ecologyHatchConfDao.getById(ecologyHatchId);
        Assert.notNull(ecologyHatchConfInDb, () -> new GlobalRunTimeException("生态熔炼配置不存在"));
        EcologyHatchExchangeConf hatchExchangeConf = ecologyHatchExchangeConfDao.getOneByEcologyHatchId(ecologyHatchConfInDb.getId());
        Assert.notNull(hatchExchangeConf, () -> new GlobalRunTimeException("兑换配置不存在"));
        Assert.isTrue(hatchExchangeConf.getStatus(), () -> new GlobalRunTimeException("兑换配置已关闭"));
        EcologyHatchUsePropsExchange ecologyHatchUsePropsExchange = ecologyHatchUsePropsExchangeDao.
                findEcologyHatchUsePropsExchangeListByPropId(propId,ecologyHatchId);
        Assert.notNull(ecologyHatchUsePropsExchange, () -> new GlobalRunTimeException("兑换消耗道具不存在"));
        EquityProps equityProps = equityPropsDao.getById(propId);
        Assert.notNull(equityProps, () -> new GlobalRunTimeException("道具卡不存在"));

        // 获取用户持有的道具数量
        Integer havingCount = userEquityPropsService.getHavingCountByUserId(ecologyHatchUsePropsExchange.getPropId(),userId);
        Integer sumCount = ecologyHatchUsePropsExchange.getPropCount() * count;
        Assert.isTrue(havingCount.intValue() >= sumCount.intValue(), () -> new GlobalRunTimeException("所需道具不足"));
        // 获取需要消耗的道具
        List<UserEquityProps> userEquityPropsList = userEquityPropsService.getHavingListByUserIdAndEquityPropsIdAndLimit(userId, ecologyHatchUsePropsExchange.getPropId(), sumCount);
        //扣除权益商品
        Assert.isTrue(userEquityPropsService.normalToconsumeByIds(userEquityPropsList, userId, "兑换" + ecologyHatchConfInDb.getCollectionName() + "熔炼上限",
                ecologyHatchConfInDb.getId(), UserEquityPropsUseTypeEnum.ECOLOGY_HATCH_CAP_CONVERTIBILITY), () -> new GlobalRunTimeException(ErrorCode.FAILED_TO_OBTAIN_EQUITY_PROPS));
        EcologyHatchCountDTO userHatchCount = ecologySmeltingService.getUserHatchCount(userId, ecologyHatchId);
        //增加熔炼上限
        userEcologyHatchUpperLimitService.addCountByUserIdAndCollectionId(userId, ecologyHatchConfInDb.getCollectionId(), count, UserEcologyHatchUpperLimitDetailsTypeEnum.EXCHANGE,
                ecologyHatchConfInDb.getCollectionName(), 0L,ecologyHatchId,userHatchCount.getSumRemainingCount());

    }


    /**
     * 给符合赠送条件的用户增加 可消耗熔炼份数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUserEcologyGiftCount(){

        //查询所有开启系统赠送的熔炼配置
        List<EcologyHatchExchangeConf> ecologyHatchGift = ecologyHatchExchangeConfDao.getEcologyHatchGift();
        if (!CollectionUtils.isEmpty(ecologyHatchGift)){
            for (EcologyHatchExchangeConf ecologyHatchExchangeConf : ecologyHatchGift) {
                EcologyHatchConf ecologyHatchConfInDb = ecologyHatchConfDao.getById(ecologyHatchExchangeConf.getEcologyHatchId());
                // 查询该熔炼下的所有用户熔炼的记录
                List<UserEcologyHatchRecords> userEcologyHatchRecordsList = userEcologyHatchRecordsDao.getAllByEcologyHatchIdList(ecologyHatchExchangeConf.getEcologyHatchId());
                if (!CollectionUtils.isEmpty(userEcologyHatchRecordsList)){
                    for (UserEcologyHatchRecords userEcologyHatchRecords : userEcologyHatchRecordsList) {
                        EcologyHatchCountDTO userHatchCount = ecologySmeltingService.getUserHatchCount(userEcologyHatchRecords.getUserId(), ecologyHatchExchangeConf.getEcologyHatchId());

                        //给用户增加可熔炼消耗上限
                        userEcologyHatchUpperLimitService.addCountByUserIdAndCollectionId(userEcologyHatchRecords.getUserId(),
                                ecologyHatchConfInDb.getCollectionId(), ecologyHatchExchangeConf.getGiftCount(), UserEcologyHatchUpperLimitDetailsTypeEnum.SYS,
                                ecologyHatchConfInDb.getCollectionName(), 0L,ecologyHatchExchangeConf.getEcologyHatchId(),userHatchCount.getSumRemainingCount());
                    };
                }

            }
        }
    }



}
